import Vue from 'vue'
import Vuex from 'vuex'
import router from "@/router";
Vue.use(Vuex)
import createPersistedState from "vuex-persistedstate";
function addNewRoute(menuList) {
  console.log(menuList)
  let routes =  router.options.routes
  console.log(routes)
  // routes.forEach(routeItem=>{
  //   if (routeItem.path=='/Index'){
  //     menuList.forEach(menu=>{
  //       let childRoute = {
  //         path:'/'+menu.menuClick,
  //         name:menu.menuName,
  //         meta:{
  //           title:'管理员管理'
  //         },
  //         component:()=>import('../components/'+menu.menuComponent)
  //       }
  //       routeItem.children.push(childRoute)
  //     })
  //   }
  // })
  //将动态路由添加进去
  for (let x of routes) {
    router.addRoute(x)
  }
}


export default new Vuex.Store({

  state: {
    menu:[]
  },
  getters: {
    getMenu(state){
      return state.menu
    }
  },
  mutations: {
    setMenu(state,menuList){
      state.menu = menuList



      addNewRoute(menuList)

    }
  },
  actions: {
  },
  modules: {
  },
  plugins: [
      createPersistedState({
        reducer:state => {
          return{
            menu:state.menu
          }
        }
      })
  ]
})
